import common.CommonFunction;
import common.Mask;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public var customerXML:XML;
public var customerList:ArrayCollection;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;

public var infoObject:Object;

public function onCreationComplete():void
{
	codeID.text = "";
	description.text = "";
	var groupArray:Array=new Array;
	for each(var obj:Object in groupDB.group)
	{
		groupArray.push(obj.@group_description.toString());
	}
	groupArray.sort();
	groupCombobox.dataProvider=groupArray;
	groupCombobox.selectedIndex=-1;
	groupCombobox.setFocus();
//	Mask.show(CommonFunction.getMessages('pleaseWait'));
}

protected function button1_clickHandler(event:MouseEvent):void
{
	groupCombobox.selectedIndex = -1;
	codeID.text = "";
	description.text = "";
	adg.dataProvider = null;
}

public function onFailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(event.message + "");
}

public function onSuccess(event:ResultEvent):void
{
	customerXML = event.result as XML;
	if(customerXML.booleanvalue == "true")
	{
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success", (Alert.OK), this, onClick);
		infoObject = new Object();
		infoObject["date"] = new Date();
		infoObject["grpCode"] = groupDB.group.(@group_description == groupCombobox.selectedLabel).@group_code;
		getCode.send(infoObject);
		codeID.text = "";
		description.text = "";
	}
	else
	{
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
}

protected function onClick(eveent:CloseEvent):void
{
	if(eveent.detail == Alert.OK)
	{
		infoObject = new Object();
		infoObject["date"] = new Date();
		infoObject["grpCode"] = groupDB.group.(@group_description == groupCombobox.selectedLabel).@group_code;
		getCode.send(infoObject);
	}
}

protected function button2_clickHandler(event:MouseEvent):void
{
	infoObject = new Object();
	infoObject["date"] = new Date();
	infoObject["grpCode"] = groupDB.group.(@group_description == groupCombobox.selectedLabel).@group_code;
	infoObject["code"] = codeID.text;
	infoObject["desc"] = description.text;
	setCode.send(infoObject);
}


protected function groupCombobox_changeHandler(event:ListEvent):void
{
	infoObject = new Object();
	infoObject["date"] = new Date();
	infoObject["grpCode"] = groupDB.group.(@group_description == groupCombobox.selectedLabel).@group_code;
	getCode.send(infoObject);
}

public function gtSuccess(event:ResultEvent):void
{
	customerXML = event.result as XML;
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, grpCode:obj.grpCode, code:obj.code, desc:obj.description});
	}
	adg.dataProvider = customerList;
}